package com.czapl.mygdxgame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.czapl.api.IFieldSelectionListener;
import com.czapl.api.ITower;
import com.czapl.api.ITower.ITowerFromMenuSelectionListener;
import com.czapl.api.IUnit;
import com.czapl.api.IUnit.IUnitSelectionListener;
import com.czapl.tabs.CUI;
import com.czapl.tabs.UIDefender;
import com.czapl.units.PegasusUnit;

public class MainScreen implements Screen , ITowerFromMenuSelectionListener, IFieldSelectionListener, IUnitSelectionListener{

	private static final float INITIAL_SPEED = 0.1f;
	private final int UNIT_COUNT = 10;
	
	private OrthographicCamera camera;
	private float mapPixelWidth, mapPixelHeight, spacerHeight;
	private CUI gameUI;
	static private GameBoard gameBoard = null;
	
	private ITower m_selectedTower = null;
	private ConstructableField m_selectedField = null;
	
	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		gameBoard.act(Gdx.graphics.getDeltaTime());
        gameBoard.draw();
		gameUI.act(Gdx.graphics.getDeltaTime());
        gameUI.draw();
        Table.drawDebug(gameUI);
        camera.update();
        
	}
	
	@Override
	public void resize(int width, int height) {
		float aspectRatio = (float)width / (float)height;
		
		camera.viewportWidth = (width/(width-UIDefender.MENU_WIDTH))*mapPixelWidth;
		camera.viewportHeight = camera.viewportWidth/aspectRatio;
		camera.position.set((camera.viewportWidth)/2, mapPixelHeight/2, 0f);
		
		float scaleY = height/camera.viewportHeight;
		float realMapPixelHeight = mapPixelHeight*scaleY;
		spacerHeight = (height-realMapPixelHeight)/2;
		
		System.out.println("w: "+ width+" h: "+ height+ " spacerH: "+ spacerHeight);
		gameUI.setViewport(width, height);
		gameUI.resize(spacerHeight);
		
		gameBoard.setCamera(camera);
	}

	@Override
	public void show() {

		gameBoard = new GameBoard(this);
		gameUI = new UIDefender(this);
		camera = new OrthographicCamera();
		mapPixelWidth = gameBoard.getMapSize().x;
		mapPixelHeight = gameBoard.getMapSize().y;
		
		InputMultiplexer multiplexer = new InputMultiplexer();
		multiplexer.addProcessor(gameUI);
		multiplexer.addProcessor(gameBoard);
		Gdx.input.setInputProcessor(multiplexer);
		
        for(int i=0; i<UNIT_COUNT; i++){
        	IUnit unit = new PegasusUnit();
        	unit.setUnitSelectionListener(this);
        	unit.setSpeed((i+1)*INITIAL_SPEED);
        	gameBoard.addUnit(unit);
        	//unit.setCellCoordinates(m_path.get(0));
        	//unit.moveToCell(m_path.get(1));
        }
        
	}
	
	@Override
	public void onTowerSelected(ITower tower) {
		System.out.println("tower selected");
		if(m_selectedField != null) {
			m_selectedField.setHighlighted(false);
			m_selectedField = null;
		}
		

		if(m_selectedTower == tower) {
			m_selectedTower = null;
			gameBoard.setFieldsHighlighted(false, true);
		}
		else {
			m_selectedTower = tower;
			gameBoard.setFieldsHighlighted(true, true);
		}
		
	}

	@Override
	public void onFieldSelected(ConstructableField field) {
		
		if(m_selectedTower != null) {
			if(field.getTower() == null) {
				gameBoard.setFieldsHighlighted(false, true);
				gameBoard.addTower(m_selectedTower, field);
				m_selectedTower = null;
			}
			return;
		}
		
		if(m_selectedField == field) {
			m_selectedField = null;
			field.setHighlighted(false);
			return;
		}
		
		if(m_selectedField != null) {
			m_selectedField.setHighlighted(false);
			field.toggleSelection();
		}
		field.setHighlighted(true);
		m_selectedField = field;
		gameUI.showInfoTab(m_selectedField);
		System.out.println("field selected ("+field.getCoordinates().getX()+","+field.getCoordinates().getY()+")");
	}
	
	@Override
	public void onUnitSelected(IUnit unit) {
		m_selectedTower = null;
		m_selectedField = null;
		gameBoard.setFieldsHighlighted(false, false);
		gameUI.showInfoTab(unit);
		System.out.println("unit selected");
		
	}	
	
	@Override
	public void hide() {
		dispose();

	}

	@Override
	public void pause() {
		// TODO to implement

	}

	@Override
	public void resume() {
		// TODO to implement

	}

	@Override
	public void dispose() {
		gameBoard.dispose();
	}

	public static GameBoard getGameBoard() {
		return gameBoard;
	}


}
